package app

import (
	"copper-cloud/pkg/e"
	"copper-cloud/pkg/logging"
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"net/http"
)

func BindAndValid(c *gin.Context, form interface{}) (int, int) {
	err := c.Bind(&form)
	if err != nil {
		return http.StatusBadRequest, e.InvalidParams
	}

	valid := validation.Validation{}
	check, checkErr := valid.Valid(form)
	if checkErr != nil {
		return http.StatusInternalServerError, e.ERROR
	}

	if !check {
		logging.ZapLogger.Infow("参数校验失败", "错误信息", valid.Errors)
		return http.StatusBadRequest, e.InvalidParams
	}

	return http.StatusOK, e.SUCCESS
}